# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
专利信息服务平台
http://search.cnipr.com/
gkx5868
sunshine12966
"""

class ZhuanLi():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded",
            "Cookie": "JSESSIONID=206B0599B46EE71CF868E5A6EF62BED7; Hm_lvt_787f052e9fd1e6c0181d2e98d4d8ddc6=1583735867; _trs_uv=k7k3g06y_1186_a8jb; _trs_ua_s_1=k7k3g06x_1186_1dgz; _gscu_719616686=83735867oe5qb725; _gscbrs_719616686=1; _gscs_719616686=8373586742j30425|pv:4; Hm_lpvt_787f052e9fd1e6c0181d2e98d4d8ddc6=1583735998",
            "DNT": "1",
            "Host": "search.cnipr.com",
            "Origin": "http://search.cnipr.com",
            "Referer": "http://search.cnipr.com/search!doOverviewSearch.action",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
        }

    def get_contents(self, pageNum):
        postData = {
            "wgViewmodle":"",
            "strWhere": "摘要=(大数据)",
            "start": pageNum,
            "limit": "10",
            "option": "2",
            "iHitPointType": "115",
            "strSortMethod": "RELEVANCE",
            "strSources": "FMZL,SYXX",
            "strSynonymous":"",
            "yuyijs":"",
            "filterChannel":"",
            "keyword2Save":"",
            "key2Save":"",
            "forward":"",
            "otherWhere":"",
            "username":"",
            "password":""
        }
        url = "http://search.cnipr.com/search!doOverviewSearch.action"
        html = HttpUtils.do_request("POST", url, self.headers, postData)
        time.sleep(1)
        selector = etree.HTML(html.text)
        root = selector.xpath('//div[@class="g_item"]')
        for s in root:
            dict_data = dict()
            dict_data['sqh'] = s.xpath('./div/ul/li[@class="g_li"]/@title')[0]  # 标题
            dict_data['flag'] = s.xpath('./div/ul/li[4]/text()')[0]  # 是否有效
            dict_data['zl_no'] = s.xpath('./div[2]/div[1]/table/tr[1]/td[1]/a/text()')[0]  # 申请号
            dict_data['zl_rq'] = s.xpath('./div[2]/div[1]/table/tr[1]/td[2]/text()')[0]  # 申请日
            dict_data['gk_no'] = s.xpath('./div[2]/div[1]/table/tr[2]/td[1]/text()')[0]  # 公开(公告)号
            dict_data['gk_rq'] = s.xpath('./div[2]/div[1]/table/tr[2]/td[2]/text()')[0]  # 公开(公告)日
            dict_data['zl_ren'] = s.xpath('./div[2]/div[1]/table/tr[4]/td/text()')[0]  # 申请(专利权)人
            dict_data['fl_no'] = s.xpath('./div[2]/div[1]/table/tr[5]/td/text()')[0]  # 分类号
            dict_data['desc'] = "".join(s.xpath('./div[2]/div[1]/table/tr[7]/td/span[2]//text()'))  # 摘要
            print(str(dict_data))


if __name__ == '__main__':
    zhuanli = ZhuanLi()
    for i in range(1, 100):
        print(f"=======第{i}页=========")
        zhuanli.get_contents(i)